﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using ApplicationGenerator.CommonModule;
using System.Web.Services.Protocols;

namespace ApplicationGenerator.CommonForms.Forms
{
    /// <summary>
    /// Formatka do wyboru metody z webserwisu
    /// </summary>
    public partial class SelectWebServiceMethodForm : Form
    {
        /// <summary>
        /// Adres URL web serwisu
        /// </summary>
        private string _webServiceURL;

        /// <summary>
        /// Nazwa wybranej metody
        /// </summary>
        public string MethodName
        {
            get;
            private set;
        }

        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="webServiceURL">Adres URL web serwisu</param>
        public SelectWebServiceMethodForm(string webServiceURL)
        {
            _webServiceURL = webServiceURL;

            InitializeComponent();
            InitializeComponent_Custom();
        }

        /// <summary>
        /// Dodatkowa inicjalizacja komponentów
        /// </summary>
        public void InitializeComponent_Custom()
        {
            if (_webServiceURL == null)
            {
                MessageBox.Show("Należy ustawić adres URL dla web serwisu", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.DialogResult = DialogResult.Abort;
                this.Close();
            }

            PrepareTreeView();
        }

        #region Methods

        /// <summary>
        /// Dodaje do drzewa metody web serwisu
        /// </summary>
        private void PrepareTreeView()
        {
            if (!File.Exists(WebServiceConfigurationHelper.GetPath(_webServiceURL)))
            {
                MessageBox.Show(string.Format("Plik {0} nie istnieje", WebServiceConfigurationHelper.GetPath(_webServiceURL)));
                DialogResult = DialogResult.Abort;
                this.Close();
            }

            Assembly dllAssembly = Assembly.LoadFile(WebServiceConfigurationHelper.GetPath(_webServiceURL));
            foreach (Type type in dllAssembly.GetTypes())
            {
                if ((typeof(SoapHttpClientProtocol)).IsAssignableFrom(type))
                {
                    try
                    {
                        MethodInfo[] list = type.GetMethods();
                        foreach (MethodInfo minfo in list)
                        {
                            if (minfo.IsDefined(typeof(SoapDocumentMethodAttribute), true))
                            {
                                treeView1.Nodes[0].Nodes.Add(minfo.Name);
                            }

                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            //if (treeView1.SelectedNode != treeView1.Nodes[0])
            {
                MethodName = treeView1.SelectedNode.Text;
            }
            /*else
            {
                methodName = "";
            }*/

            this.Close();
        }
        #endregion



    }
}
